<?php
class CastDictionaryUpdateAction extends BaseAction 
{
    /**
     * @isnull false
     * @label Cast Dictionary Id
     */
    public $id;

    /**
     * @isnull false
     * @maxLength 255
     * @label Master Name
     */
    public $master;
    public $chi_last_name;
    public $chi_first_name;
    public $eng_last_name;
    public $eng_first_name1;
    public $eng_first_name2;
    public $others;
    public $see_also;
    public $remark;
    public $gender;
    public $is_chinese;
    public $modified_at;

    public $reset;
    public $ut;

    function execute()
    {
        #reset
        if ($this->reset) {
            return 'reset';
        }

        #error
		if(count($this->_errors) > 0){
			$this->init_page_data();
			return 'filter-error';
		}

        #check concurrent
        if (CommonUtils::is_concurrent('cast_dictionary', $this->id, $this->modified_at) && empty($this->ut)) {
            $this->init_page_data();
            $this->addError('concurrent', array('0105', 'Concurrent update found!'));
            return 'logic-error';
        }

        #check master name
        if (cast_dictionary::checkExistsByMasterName($this->master, $this->id)) {
            $this->init_page_data();
            $this->addError('Master name', array('0024' => 'Master name error'));
            return 'filter-error';
        }

        #save
        try {
            $this->update_cast_dictionary();
            return 'succ';
        } catch (Exception $e) {
            return 'system-error';
        }
    }

    private function update_cast_dictionary()
    {
        $cast_dictionary = new cast_dictionary;
        $cast_dictionary = $cast_dictionary->get($this->id);
        $cast_dictionary = Model::diff($cast_dictionary, $this);
        $cast_dictionary->modified_at = date('Y-m-d H:i:s');
        $cast_dictionary->modified_by = CommonUtils::get_user();

        $rs = Model::$dbo['w']->save($cast_dictionary);
        if ($rs <= 0) {
			$this->addError("cast_dictionary:".AC::LOG_OP_UPDATE.":".$this->id, "update cast_dictionary failed", json_encode($cast_dictionary));
        }
    }

    private function init_page_data()
    {
        $this->setData();
    }
}
